/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.math.ColorRGBA;
import com.jme3.niftygui.NiftyJmeDisplay;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.NiftyEventSubscriber;
import de.lessvoid.nifty.controls.ConsoleCommands;
import de.lessvoid.nifty.controls.ConsoleCommands.ConsoleCommand;
import de.lessvoid.nifty.controls.ConsoleExecuteCommandEvent;
import de.lessvoid.nifty.controls.console.ConsoleControl;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;
import de.lessvoid.nifty.tools.Color;

/**
 * @date 2024年7月17日14点02分
 * @author JohnKkk
 */
public class NiftyGuiConsole extends SimpleApplication implements ScreenController{

    private Nifty m_Nifty;
    private boolean m_DisplayFPS = true;
    private boolean m_DisplayStat = true;
    
    public static void main(String[] args) {
        NiftyGuiConsole niftyGuiLabel = new NiftyGuiConsole();
        niftyGuiLabel.start();
    }
    
    @Override
    public void simpleInitApp() {
        // 设置默认背景色
        getViewPort().setBackgroundColor(ColorRGBA.DarkGray);
        
        // 初始化Nifty
        NiftyJmeDisplay niftyDisplay = NiftyJmeDisplay.newNiftyJmeDisplay(
                assetManager,
                inputManager,
                audioRenderer,
                guiViewPort);
        m_Nifty = niftyDisplay.getNifty();
        // 将NiftyGUI显示对象添加到JME3中
        guiViewPort.addProcessor(niftyDisplay);
        
        m_Nifty.fromXml("Interface/NiftyGuiConsole.xml", "GScreen0");
        
        // 禁用flyCam并显示鼠标
        flyCam.setEnabled(false);
        inputManager.setCursorVisible(true);
    }
    
    @NiftyEventSubscriber(id="console0")
    public final void onConsoleCommand(final String id, final ConsoleExecuteCommandEvent consoleExecuteCommandEvent){
        System.out.println("command:" + consoleExecuteCommandEvent.getCommand() + ";commandLine:" + consoleExecuteCommandEvent.getCommandLine());
    }
    
    /**
     * 创建一个SimpleCommand.<br/>
     */
    private class SimpleCommand implements ConsoleCommand {

        @Override
        public void execute(String... arg0) {
            System.out.println("SimpleCommand执行:" + arg0[0]);
        }
        
    }
    /**
     * StatCommand用于处理与Stat相关的命令.<br/>
     */
    private class StatCommand implements ConsoleCommand{

        @Override
        public void execute(String... arg0) {
            if(arg0.length >= 2 && arg0[0].equals("stat")){
                if(arg0[1].equals("fps")){
                    m_DisplayFPS = !m_DisplayFPS;
                    System.out.println("stat fps:" + m_DisplayFPS);
                }
                else if(arg0[1].equals("unit")){
                    m_DisplayStat = !m_DisplayStat;
                    System.out.println("stat unit:" + m_DisplayStat);
                }
            }
        }
        
    }

    @Override
    public void bind(Nifty nfity, Screen screen) {
        // 获取控制台对象控制器
        // 等同于screen.findElementById("console0").getNiftyControl(ConsoleControl.class)
        ConsoleControl consoleControl = screen.findNiftyControl("console0", ConsoleControl.class);
        
        // 输出一些内容到控制台中(你可以试试其他output方法,比如outputError)
        consoleControl.output("begin game...", new Color(255, 0, 0, 255));
        
        // 创建ConsoleCommands对象来关联我们想要的命令
        ConsoleCommands consoleCommands = new ConsoleCommands(nfity, consoleControl);
        
        // 添加一些命令绑定
        SimpleCommand simpleCommand = new SimpleCommand();
        consoleCommands.registerCommand("testCommand", simpleCommand);
        StatCommand statFPSCommand = new StatCommand();
        StatCommand statUnitCommand = new StatCommand();
        consoleCommands.registerCommand("stat fps", statFPSCommand);
        consoleCommands.registerCommand("stat unit", statUnitCommand);
        
        // 完成命令绑定
        consoleCommands.enableCommandCompletion(true);
    }

    @Override
    public void onStartScreen() {
    }

    @Override
    public void onEndScreen() {
    }
    
}
